package servlet.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import pojo.User;
import pojo.User2;
import service.UserLoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/adminupdateuser")
public class UpdateUser extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StringBuilder sb = new StringBuilder();
        try (BufferedReader reader = req.getReader()) {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        }
        String jsonString = sb.toString();

        // 使用Fastjson解析JSON字符串为Java对象
        User2 user = JSON.parseObject(jsonString, User2.class);

        System.out.println(user.getId());
        UserLoginService userLoginService = new UserLoginService();
        int i = userLoginService.updateUserAdmin(user.getId(), user.getUsername(), user.getPassword(), user.getPhone(), user.getEmail(), user.getAddress(), user.getIsAdmin());
        System.out.println(i);
        resp.setContentType("application/json;charset=utf-8");
        JSONObject jsonResponse = new JSONObject();
        jsonResponse.put("code", 1); // 添加books列表
        String jsonString2 = jsonResponse.toJSONString();
        PrintWriter writer = resp.getWriter();
        writer.write(jsonString2);
    }
}
